﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TouchSocket.Core;
using TouchSocket.Core.ByteManager;
using TouchSocket.Rpc.TouchRpc;
using TouchSocket.Sockets;
using TouchSocket.Core.Log;
using System.IO;

namespace Updater.Core
{
    /// <summary>
    /// 默认的业务处理
    /// </summary>
    public class DefaultServerEvent : IServerEvent
    {
        public void Connected(TcpTouchRpcSocketClient client, TouchSocketEventArgs e)
        {


            client.Logger.Message($"用户连接完成ID={client.ID}.IP={client.IP},port={client.Port}");
        }

        public void Connecting(TcpTouchRpcSocketClient client, ClientOperationEventArgs e)
        {
            client.Logger.Message($"用户请求连接ID={client.ID},IP={client.IP},port={client.Port}");
        }

        public void Disconnected(TcpTouchRpcSocketClient client, ClientDisconnectedEventArgs e)
        {
            client.Logger.Message($"用户断开连接,ID={client.ID}");
        }

        public void FileTransfered(TcpTouchRpcSocketClient client, FileTransferStatusEventArgs e)
        {
            //传输结束，但是不一定成功，需要从e.Result判断状态
            client.Logger.Message($"客户端传输文件结束，ID={client.ID}，请求类型={e.TransferType}，文件名={e.FileRequest.Path}，请求状态={e.Result}");
            


        }

        public void FileTransfering(TcpTouchRpcSocketClient client, FileOperationEventArgs e)
        {
            //有可能是上传，也有可能是下载
            client.Logger.Message($"有客户端请求传输文件，ID={client.ID}，请求类型={e.TransferType}，请求文件名={e.FileRequest.Path}");
            if (e.TransferType==TransferType.Push)
            {
                if (!e.Metadata.ContainsKey("token"))
                {
                    e.IsPermitOperation = false;
                    e.Message = "禁止上传！";
                    return;
                } 
                if (e.Metadata["token"]!="123")
                {
                    e.Message = "禁止上传！token";
                    e.IsPermitOperation = false;
                }
                FileInfo fileInfo = new FileInfo(e.FileRequest.SavePath);
                //确保目录存在
                FileHelper.SureDirExistence(fileInfo.Directory.FullName);
            }
           
        }

        public void Handshaked(TcpTouchRpcSocketClient client, VerifyOptionEventArgs e)
        {
            client.Logger.Message("");
        }

        public void Handshaking(TcpTouchRpcSocketClient client, VerifyOptionEventArgs e)
        {
            client.Logger.Message("");
        }

        public void IDChanged(TcpTouchRpcSocketClient client, TouchSocketEventArgs e)
        {
            client.Logger.Message("");
        }

        public void Received(TcpTouchRpcSocketClient client, short protocol, ByteBlock byteBlock)
        {
            client.Logger.Message($"有信息到达ID={client.ID},IP={client.IP},Port={client.Port},数据大小={byteBlock.Length}");
        }

        public void StreamTransfered(TcpTouchRpcSocketClient client, StreamStatusEventArgs e)
        {
            client.Logger.Message("");
        }

        public void StreamTransfering(TcpTouchRpcSocketClient client, StreamOperationEventArgs e)
        {
            client.Logger.Message("");
        }
    }
}
